# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=setuptools
pkgbase=mingw-w64-python-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-setuptools" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
pkgver=41.4.0
pkgrel=1
pkgdesc="Easily download, build, install, upgrade, and uninstall Python packages (mingw-w64)"
arch=('any')
license=('PSF')
url="https://pypi.python.org/pypi/setuptools"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2>=2.7"
             "${MINGW_PACKAGE_PREFIX}-python2-appdirs"
             "${MINGW_PACKAGE_PREFIX}-python2-certifi"
             "${MINGW_PACKAGE_PREFIX}-python2-ordered-set"
             "${MINGW_PACKAGE_PREFIX}-python2-packaging"
             "${MINGW_PACKAGE_PREFIX}-python2-pyparsing"
             "${MINGW_PACKAGE_PREFIX}-python2-six"            
             "${MINGW_PACKAGE_PREFIX}-python2-wincertstore"
             "${MINGW_PACKAGE_PREFIX}-python3>=3.3"
             "${MINGW_PACKAGE_PREFIX}-python3-appdirs"
             "${MINGW_PACKAGE_PREFIX}-python3-certifi"
             "${MINGW_PACKAGE_PREFIX}-python3-ordered-set"
             "${MINGW_PACKAGE_PREFIX}-python3-packaging"
             "${MINGW_PACKAGE_PREFIX}-python3-pyparsing"
             "${MINGW_PACKAGE_PREFIX}-python3-six"
             "${MINGW_PACKAGE_PREFIX}-python3-wincertstore"
             'git')
depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python3")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/pypa/setuptools/archive/v${pkgver}.tar.gz
        '0001-mingw-python-fix.patch'
        '0002-Allow-usr-bin-env-in-script.patch'
        '0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch'
        '0004-dont-execute-msvc.patch'
        '0005-execv-warning.patch')
#_checkdeps=('mock' 'pytest-flake8' 'virtualenv' 'pytest-virtualenv' 'pytest'
#            'wheel' 'coverage' 'pytest-cov')
#checkdepends=("${_checkdeps[@]/#/${MINGW_PACKAGE_PREFIX}-python3-}" "${_checkdeps[@]/#/${MINGW_PACKAGE_PREFIX}-python2-}"
#              "${MINGW_PACKAGE_PREFIX}-python3-paver"
#              "${MINGW_PACKAGE_PREFIX}-python2-futures" 'git')
sha256sums=('185d519d6b283a10008427402cc3f834878759da279e5100b7810bc5f772ee1c'
            'd3bc778723e63bbd6e43ab3536a015c547c8c20a95c6679a952284a7a4822996'
            '7bb5617c69566f5fbf1a3ee29a08179e1b41bdeabbc2998bf67615e9aa000424'
            '0e556505cb70ff3a5df856e352d5e2b3cf1582c25d02fc00a2d935a28576b28c'
            '2a854e21e99d6724999e2abeff08451923e9940b4092eb2b638702da17abdb73'
            'f8db482721900dbbfd19f52b9042954c85b9e3a61a5e4b68a82aeafec8cba34a')

prepare() {
  export SETUPTOOLS_INSTALL_WINDOWS_SPECIFIC_FILES=1
  cd "${srcdir}/setuptools-${pkgver}"
  patch -p1 -i ${srcdir}/0001-mingw-python-fix.patch
  patch -p1 -i ${srcdir}/0002-Allow-usr-bin-env-in-script.patch
  patch -p1 -i ${srcdir}/0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch
  # TODO: This one is fixed now in python2/3 itself, so this can be dropped
  # in the future: https://github.com/msys2/MINGW-packages/issues/5155
  patch -p1 -i ${srcdir}/0004-dont-execute-msvc.patch
  patch -p1 -i ${srcdir}/0005-execv-warning.patch

  cd "${srcdir}"

  # Compile our own MSYS2-layout and /usr/bin/env capable {cli,gui}{-32,-64}.exe to replace the precompiled binaries.
  # .. when arm is ready, add it to this.
  PATH=/mingw32/bin:$PATH gcc -DGUI=0           -O -s -o setuptools-${pkgver}/setuptools/cli-32.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw32/bin:$PATH gcc -DGUI=1 -mwindows -O -s -o setuptools-${pkgver}/setuptools/gui-32.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw64/bin:$PATH gcc -DGUI=0           -O -s -o setuptools-${pkgver}/setuptools/cli-64.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw64/bin:$PATH gcc -DGUI=1 -mwindows -O -s -o setuptools-${pkgver}/setuptools/gui-64.exe setuptools-${pkgver}/launcher.c

  rm -r setuptools-$pkgver/{pkg_resources,setuptools}/{extern,_vendor}

  # Upstream devendoring logic is badly broken, see:
  # https://bugs.archlinux.org/task/58670
  # https://github.com/pypa/pip/issues/5429
  # https://github.com/pypa/setuptools/issues/1383
  # The simplest fix is to simply rewrite import paths to use the canonical
  # location in the first place
  for _module in setuptools pkg_resources '' ; do
     find setuptools-$pkgver/$_module -name \*.py -exec sed -i \
         -e 's/from '$_module.extern' import/import/' \
         -e 's/from '$_module.extern'./from /' \
         -e 's/import '$_module.extern'./import /' \
         -e "s/__import__('$_module.extern./__import__('/" \
         {} +
   done

  # Remove post-release tag since we are using stable tags
  sed -e '/tag_build = .post/d' \
      -e '/tag_date = 1/d' \
      -i setuptools-$pkgver/setup.cfg

  # 'Clean' installation is expected to fail since we removed bundled packages
  #sed -i '/^def test_clean_env_install/i import pytest\n\n@pytest.mark.xfail' setuptools-$pkgver/setuptools/tests/test_virtualenv.py

  # Tests failed. Importing an unbundled new setuptools in a virtualenv does not work, but this won't
  # affect normal virtualenv usage (which don't have to import the unbundled setuptools in *current*
  # dir.
  sed -e '/^def test_pip_upgrade_from_source/i @pytest.mark.xfail' \
      -e '/^def test_test_command_install_requirements/i @pytest.mark.xfail' \
      -e '/^def test_no_missing_dependencies/i @pytest.mark.xfail' \
      -i setuptools-$pkgver/setuptools/tests/test_virtualenv.py

  cp -r setuptools-${pkgver} setuptools-python2-${CARCH}
  cp -r setuptools-${pkgver} setuptools-python3-${CARCH}

  cd "${srcdir}"/setuptools-python2-${CARCH}
  sed -i -e "s|^#\!.*/usr/bin/python|#!${MINGW_PREFIX}/bin/python2|" tests/manual_test.py
  sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python2|" setuptools/command/easy_install.py

  cd "${srcdir}"/setuptools-python3-${CARCH}
  sed -i -e "s|^#\!.*/usr/bin/python|#!${MINGW_PREFIX}/bin/python3|" tests/manual_test.py
  sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python3|" setuptools/command/easy_install.py
}

build() {
  # Build python 2 module
  cd "${srcdir}"/setuptools-python2-${CARCH}
  ${MINGW_PREFIX}/bin/python2 bootstrap.py
  ${MINGW_PREFIX}/bin/python2 setup.py build
  rm -rf build/lib/pkg_resources/_vendor
  rm -rf build/lib/setuptools/_vendor

  # Build python 3 module
  cd "${srcdir}"/setuptools-python3-${CARCH}
  ${MINGW_PREFIX}/bin/python3 bootstrap.py
  ${MINGW_PREFIX}/bin/python3 setup.py build
  rm -rf build/lib/pkg_resources/_vendor
  rm -rf build/lib/setuptools/_vendor
}

#check() { 
#  # Workaround UTF-8 tests by setting LC_CTYPE
#  export LC_CTYPE=en_US.UTF-8

#  # https://github.com/pypa/setuptools/pull/810
#  export PYTHONDONTWRITEBYTECODE=1

#  cd "${srcdir}"/setuptools-python3-${CARCH}
#  ${MINGW_PREFIX}/bin/python3 setup.py pytest

#  cd "${srcdir}"/setuptools-python2-${CARCH}
#  ${MINGW_PREFIX}/bin/python2 setup.py pytest
#}

package_python3-setuptools() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3>=3.3"
           "${MINGW_PACKAGE_PREFIX}-python3-packaging"
           "${MINGW_PACKAGE_PREFIX}-python3-pyparsing"
           "${MINGW_PACKAGE_PREFIX}-python3-ordered-set"
           "${MINGW_PACKAGE_PREFIX}-python3-appdirs"
           "${MINGW_PACKAGE_PREFIX}-python3-six")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-certifi: Mozilla's CA Bundle support"
              "${MINGW_PACKAGE_PREFIX}-python2-wincertstore: Windows' cert store support")
  install=${_realname}3-${CARCH}.install

  local _mingw_prefix=$(cygpath -wm ${MINGW_PREFIX})
  local _py3_base=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  cd "${srcdir}/setuptools-python3-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  # Move files conflicted with python2-setuptools
  mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install-script.py ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe
  [[ -f ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe.manifest ]] && {
    mv ${pkgdir}${MINGW_PREFIX}/bin/easy_install.exe.manifest ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe.manifest
    sed -e "s|easy_install|easy_install-${_py3_base}|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}.exe.manifest
  }
  sed -e "s|${_mingw_prefix}/bin/|${MINGW_PREFIX}/bin/|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py3_base}-script.py
}

package_python2-setuptools() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2>=2.7"
           "${MINGW_PACKAGE_PREFIX}-python2-packaging"
           "${MINGW_PACKAGE_PREFIX}-python2-pyparsing"
           "${MINGW_PACKAGE_PREFIX}-python2-ordered-set"
           "${MINGW_PACKAGE_PREFIX}-python2-appdirs"
           "${MINGW_PACKAGE_PREFIX}-python2-six")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-certifi: Mozilla's CA Bundle support"
              "${MINGW_PACKAGE_PREFIX}-python2-wincertstore: Windows' cert store support")

  install=${_realname}2-${CARCH}.install
  local _mingw_prefix=$(cygpath -wm ${MINGW_PREFIX})
  local _py2_base=$(${MINGW_PREFIX}/bin/python2 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  cd "${srcdir}/setuptools-python2-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  sed -e "s|${_mingw_prefix}/bin/|${MINGW_PREFIX}/bin/|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-script.py
  sed -e "s|${_mingw_prefix}/bin/|${MINGW_PREFIX}/bin/|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-${_py2_base}-script.py
}

# Wrappers
package_mingw-w64-i686-python2-setuptools() {
  package_python2-setuptools
}

package_mingw-w64-i686-python3-setuptools() {
  package_python3-setuptools
}

package_mingw-w64-x86_64-python2-setuptools() {
  package_python2-setuptools
}

package_mingw-w64-x86_64-python3-setuptools() {
  package_python3-setuptools
}
